﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DBHelper;
using SME.Food.Surperise.Model;

namespace SME.Food.Surperise.DAL
{
	/// <summary>
	/// 数据访问类:Menus_Dishes
	/// </summary>
	public partial class Menus_Dishes_DAL
	{
        public Menus_Dishes_DAL()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ID", "Menus_Dishes"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Menus_Dishes");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString());
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
        public int Add(Menus_Dishes_Model model)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.AppendFormat(@"insert into Menus_Dishes(Menus_No,Dishes_No,Num,Remark,Dishes_Price) values ('{0}','{1}',{2},'{3}',{4})", model.Menus_No, model.Dishes_No, model.Num, model.Remark,model.Dishes_Price);
			int obj = DbHelperSQL.ExcuteNoQuery(strSql.ToString());
			if (obj >0)
			{
				return obj;
			}
			else
			{
				return  -1;
			}
		}

        /// <summary>
        /// 增加多条数据
        /// </summary>
        public bool Add(List<Menus_Dishes_Model> modelList)
        {
            int i = 0;
            if (modelList != null && modelList.Count > 0)
            {
                foreach (Menus_Dishes_Model model in modelList)
                {

                    i += Add(model);

                }
            }
            if (i == modelList.Count)
                return true;
            else  return false ;
        }

        public bool Update(Bas_Menus_Model model, List<Menus_Dishes_Model> addList, string delids, List<Menus_Dishes_Model> updateList)
        {
            StringBuilder menusUpdateSql = new StringBuilder();
            StringBuilder dishesAddSql = new StringBuilder();
            StringBuilder dishesdelSql = new StringBuilder();
            StringBuilder dishesUpdateSql = new StringBuilder();
            bool i = true, j = true, m = true, n = true;
            if (model != null)
            {
                menusUpdateSql.AppendFormat(@"update Bas_Menus set TotalNum={0}, TotalMony={1} ,Remark='{2}' where Menus_No='{3}'"
                    , model.TotalNum, model.TotalMony, model.Remark, model.Menus_No);
                 i = (DbHelperSQL.ExcuteNoQuery(menusUpdateSql.ToString()) > 0);

            }
            if (addList.Count > 0)
            {
                j=Add(addList);

            }
            if (delids.Length>0)
            {
                string[] ids = delids.Split('|');
                m= Delete(ids[0],ids[1]);
            }
            if (updateList.Count > 0)
            {
                foreach (Menus_Dishes_Model updatemodel in updateList)
                {
                   n=  Update(updatemodel);
                }
            }
            if (i & j & m & n)
                return true;
            else 
            return false;
        }

		/// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(Menus_Dishes_Model model)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.AppendFormat("update Menus_Dishes set Num={0} where Menus_No={1} and Dishes_No={2}",model.Num,model.Menus_No,model.Dishes_No);
			
			int rows=DbHelperSQL.ExcuteNoQuery(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        /// <summary>
        /// 编辑菜单时删除菜品
        /// </summary>
        /// <param name="dishesNo"></param>
        /// <param name="menusNo"></param>
        /// <returns></returns>
	    public bool Delete(string dishesNo,string menusNo)
	    {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Menus_Dishes where Dishes_No in (" + dishesNo + ") and Menus_No ='" + menusNo + "'");
            int rows = DbHelperSQL.ExcuteNoQuery(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

	    /// <summary>
		///删除菜单时同时删除菜品
		/// </summary>
		public bool Delete(string  ID)
		{
		    
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Menus_Dishes where  Menus_No ='"+ID+"'");
			int rows=DbHelperSQL.ExcuteNoQuery(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Menus_Dishes ");
            strSql.Append(" where Menus_No in (" + IDlist + ")  ");
			int rows=DbHelperSQL.ExcuteNoQuery(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public Menus_Dishes_Model GetModel(string menuDishesNo)
		{
		    string[] ids = menuDishesNo.Split('|');
			StringBuilder strSql=new StringBuilder();
            strSql.Append(@"select  top 1 ID,Menus_No,Dishes_No,Num,Remark,Dishes_Price from Menus_Dishes 
                       where Dishes_No='" + ids[0] + "' and Menus_No='"+ids[1]+"'");

            Menus_Dishes_Model model = new Menus_Dishes_Model();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public Menus_Dishes_Model DataRowToModel(DataRow row)
		{
            Menus_Dishes_Model model = new Menus_Dishes_Model();
			if (row != null)
			{
				if(row["ID"]!=null && row["ID"].ToString()!="")
				{
					model.ID=int.Parse(row["ID"].ToString());
				}
				if(row["Menus_No"]!=null)
				{
					model.Menus_No=row["Menus_No"].ToString();
				}
				if(row["Dishes_No"]!=null)
				{
					model.Dishes_No=row["Dishes_No"].ToString();
				}
				if(row["Num"]!=null && row["Num"].ToString()!="")
				{
					model.Num=int.Parse(row["Num"].ToString());
				}
				if(row["Remark"]!=null)
				{
					model.Remark=row["Remark"].ToString();
				}
                if (row["Dishes_Price"] != null && row["Dishes_Price"].ToString() != "")
				{
                    model.Dishes_Price =decimal.Parse(row["Dishes_Price"].ToString());
				}
                
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select ID,Menus_No,Dishes_No,Num,Remark,Dishes_Price ");
			strSql.Append(" FROM Menus_Dishes ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		
		

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

